<%@ page language="java" %>
<%@ page session="true" %>
<%@ page import="java.sql.*,java.io.*,com.jobportal.MyFunctions"%>

<HEAD>
	<script LANGUAGE="Javascript" SRC="Images/validate.js"></script>
	<LINK href="styles.css" type="text/css" rel="stylesheet">
</HEAD>
<BODY class="Grad">
<h3 align="center">Apply Job</h3>
<BR><BR><BR><BR><BR><BR>
<%
	Connection con=null;
	ResultSet rs=null;
	Statement stmt=null,stmt1=null;
	MyFunctions MF = new MyFunctions();
	String ApplicationID = request.getParameter("ApplicationID");
	ApplicationID = MF.genNextID("applicationdetails","ApplicationID","A-");
	String userid = request.getParameter("userid");
	String JobID = request.getParameter("JobID");
	Date ApplicationDate = null;
	ApplicationDate = new Date(System.currentTimeMillis());
	String Status = "Applied",AppID="";	
	try{

			Integer objauth = (Integer)session.getAttribute("auth");
			int auth = objauth.intValue();
			System.out.println("value of auth is :"+auth);
			
			if(auth==0 || auth==2){
			%>
			<HTML>
				<HEAD>
					<Title>Untitled</Title>
				</HEAD>
				
				<BODY>
					<div align="center">
						<FONT COLOR="red" face="Times new roman"><I>
						u r not authorized user to apply for the job</I><FONT></br></br>
						<INPUT TYPE="Button" onclick="window.close()" value="Close" Class="Button">
					
				</div></BODY>
			</HTML>
				
			<%}else{
		/* Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			con = DriverManager.getConnection("jdbc:odbc:JobPortal","root",""); */

			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jobmiller","root","root");

			stmt =  con.createStatement();
			stmt1 =  con.createStatement();
			String ChkQry = "Select ApplicationID from applicationdetails where userid='"+userid+"' and JobID='"+JobID+"'";
			rs = stmt1.executeQuery(ChkQry);
			
			if(rs==null){
				AppID = "";
			}
			else if(rs.next()){
				AppID = rs.getString(1);
			}
			if(!(AppID==null||AppID.equals("")))
			{
					%>
						<P align=center><FONT size="+1" COLOR="red" face="Times new roman"><I>You have already applied for this job</I></FONT> <BR><BR>
						<INPUT TYPE="Button" onclick="window.close()" value="Close" Class="Button">
						</P>
					<%
			}
			else
			{
				String Query = "Insert into applicationdetails values('"+ApplicationID+"','"+userid+"','"+JobID+"','"+ApplicationDate+"','"+Status+"')";
				int result = stmt.executeUpdate(Query);
				if( result > 0)	{
					%>
						<P align=center><FONT COLOR="green" face="Times new roman"><I>You have successfully applied for the job</I></FONT> <BR><BR>
						<INPUT TYPE="Button" onclick="window.close()" value="Close" Class="Button">

						</P>
					<%
				}
				else{
					%><P align=center>Error: Please try again<BR>
					 
					 </P>
					<% 
				}
			}
			stmt.close();
			stmt1.close();
			con.close();
		}}catch(Exception e){
			stmt.close();
			stmt1.close();
			con.close();
			%><%=e%><%
		}
	
%>
</BODY>

</PRE>